home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 23 / CU Amiga - Super CD-ROM 23 (June 1998).iso / CUCD / Magazine / C_Tutorial / Part-10 / frac0 / idcmp.c < prev    next >
C/C++ Source or Header  |  1998-02-28  |  9KB  |  362 lines

  1. #include "idcmp.h"
  2. #include "drawwin.h"
  3. #include "gadgets.h"
  4. #include "loadsave.h"
  5. #include "menu.h"
  6. #include "toolwin.h"
  7. #include "arexx.h"
  8. #include "fractal.h"
  9.  
  10. #include<dos/rdargs.h>
  11.  
  12. #include<string.h>
  13. #include<stdio.h>
  14.  
  15. #include<clib/dos_protos.h>
  16. #include<clib/exec_protos.h>
  17. #include<clib/gadtools_protos.h>
  18. #include<clib/graphics_protos.h>
  19. #include<clib/intuition_protos.h>
  20.  
  21. static void doGadgetUp(struct Window*, UWORD, struct Gadget*);
  22. static int  doMenuPick(struct Window*, UWORD);
  23. static int  doARexx(struct RexxMsg*, struct Window*);
  24.  
  25. static void new(struct Window*);
  26.  
  27. static int  isCommand(char*, char*, char*, LONG*);
  28. static void freeCommand(void);
  29.  
  30. /* Our message handling code */
  31. void handleIDCMP()
  32. {
  33.     char* text = "Hello World!";
  34.     int going = TRUE;
  35.     int drawing = FALSE;
  36.     ULONG drawsig, toolsig, arexxsig, gotsig;
  37.     struct Window* drawwin = getDrawWin();
  38.     drawsig = 1 << drawwin->UserPort->mp_SigBit;
  39.     arexxsig = getARexxSig();
  40.     while(going)
  41.     {
  42.         struct IntuiMessage* intuimsg;
  43.         /* Only include tool window signal mask if window is open */
  44.         toolsig = getToolSig();
  45.         /* Wait for messages to arrive */
  46.         gotsig = Wait(drawsig | toolsig | arexxsig);
  47.         /* Messages have arrived: loop through all of them */
  48.         /* Check messages from the drawing window first */
  49.         if(gotsig & drawsig)
  50.         {
  51.             while(intuimsg = GT_GetIMsg(drawwin->UserPort))
  52.             {
  53.                 /* Copy the important bits of the message */
  54.                 ULONG class = intuimsg->Class;
  55.                 UWORD code = intuimsg->Code;
  56.                 WORD mousex = intuimsg->MouseX;
  57.                 WORD mousey = intuimsg->MouseY;
  58.                 /* Reply when finished copying bits from message */
  59.                 GT_ReplyIMsg(intuimsg);
  60.                 /* Act on this message... */
  61.                 switch(class)
  62.                 {
  63.                 case IDCMP_MOUSEBUTTONS:
  64.                     switch(code)
  65.                     {
  66.                     case SELECTDOWN:
  67.                         drawing = TRUE;
  68.                         break;
  69.                     case SELECTUP:
  70.                         drawing = FALSE;
  71.                         break;
  72.                     }
  73.                     /* break; omitted so we draw on click, too */
  74.                 case IDCMP_MOUSEMOVE:
  75.                     if(drawing)
  76.                     {
  77.                         Move(drawwin->RPort, mousex, mousey);
  78.                         Text(drawwin->RPort, text, strlen(text));
  79.                     }
  80.                     break;
  81.                 case IDCMP_MENUPICK:
  82.                     going = doMenuPick(drawwin, code);
  83.                     drawwin = getDrawWin();
  84.                     drawsig = 1 << drawwin->UserPort->mp_SigBit;
  85.                     break;
  86.                 }
  87.             }
  88.         }
  89.         /* Now check messages from the tool window */
  90.         if(going && (gotsig & toolsig))
  91.         {
  92.             struct Window* toolwin = getToolWin();
  93.             while(toolwin && (intuimsg = GT_GetIMsg(toolwin->UserPort)))
  94.             {
  95.                 /* Copy the important bits of the message */
  96.                 ULONG class = intuimsg->Class;
  97.                 UWORD code = intuimsg->Code;
  98.                 APTR iaddr = intuimsg->IAddress;
  99.                 /* Reply when finished copying bits from message */
  100.                 GT_ReplyIMsg(intuimsg);
  101.                 /* Act on this message... */
  102.                 switch(class)
  103.                 {
  104.                 case IDCMP_CLOSEWINDOW:
  105.                     closeToolWin();
  106.                     /* Update our local toolwin, so we stop loop */
  107.                     toolwin = NULL;
  108.                     uncheckToolBar(drawwin);
  109.                     break;
  110.                 case IDCMP_REFRESHWINDOW:
  111.                     /* You *MUST* remember to ask for and handle these refresh messages */
  112.                     GT_BeginRefresh(toolwin);
  113.                     GT_EndRefresh(toolwin, TRUE);
  114.                     break;
  115.                 case IDCMP_GADGETUP:
  116.                     doGadgetUp(drawwin, code, (struct Gadget*)iaddr);
  117.                     break;
  118.                 case IDCMP_MENUPICK:
  119.                     going = doMenuPick(drawwin, code);
  120.                     /* Update our local toolwin, so we stop loop */
  121.                     toolwin = getToolWin();
  122.                     drawwin = getDrawWin();
  123.                     drawsig = 1 << drawwin->UserPort->mp_SigBit;
  124.                     break;
  125.                 }
  126.             }
  127.         }
  128.         /* Now check messages from the ARexx port */
  129.         if(going && (gotsig & arexxsig))
  130.         {
  131.             struct RexxMsg* msg;
  132.             while(going && (msg = getARexxMsg()))
  133.                 going = doARexx(msg, drawwin);
  134.         }
  135.     }
  136. }
  137.  
  138. /* Process IDCMP_GADGETUP event */
  139. static void doGadgetUp(struct Window* drawwin, UWORD code, struct Gadget* gad)
  140. {
  141.     switch(gad->GadgetID)
  142.     {
  143.     case MYBUT_ID:
  144.         /* Our button was clicked!  Set foreground to next pen colour */
  145.         nextFgPen(drawwin);
  146.         break;
  147.     case MYPAL_ID:
  148.         /* Our palette gadget was clicked!  Set foreground to gadget colour */
  149.         setFgPen(drawwin, code);
  150.         break;
  151.     }
  152. }
  153.  
  154. /* Process IDCMP_MENUPICK event */
  155. static int doMenuPick(struct Window* drawwin, UWORD code)
  156. {
  157.     UWORD menuCode, menuNumber, itemNumber;
  158.     /* Loop over all the menu selections in the menu code */
  159.     struct MenuItem* item;
  160.     for(menuCode = code;
  161.             menuCode != MENUNULL;
  162.             menuCode = item->NextSelect)
  163.     {
  164.         item = ItemAddress(drawwin->MenuStrip, menuCode);
  165.         /* Extract the menu number and menu item number from the menu code */
  166.         menuNumber = MENUNUM(menuCode);
  167.         itemNumber = ITEMNUM(menuCode);
  168.         /* Now decide what to do based on what menu item was selected */
  169.         switch(menuNumber)
  170.         {
  171.         case 0:  /* Project menu */
  172.             switch(itemNumber)
  173.             {
  174.             case 0:  /* Load */
  175.                 return load();
  176.             case 1:  /* Save */
  177.                 save();
  178.                 break;
  179.             case 3:  /* New (item 2 is the bar!) */
  180.                 new(drawwin);
  181.                 break;
  182.             case 5:  /* Quit (item 4 is the bar!) */
  183.                 return FALSE;
  184.             }
  185.             break;
  186.         case 1:  /* Pen menu */
  187.             switch(itemNumber)
  188.             {
  189.             case 0:  /* Next */
  190.                 nextFgPen(drawwin);
  191.                 break;
  192.             case 1:  /* Prev */
  193.                 prevFgPen(drawwin);
  194.                 break;
  195.             case 3:  /* Reset (item 2 is the bar!) */
  196.                 resetFgPen(drawwin);
  197.                 break;
  198.             }
  199.             break;
  200.         case 2:  /* Tools menu */
  201.             switch(itemNumber)
  202.             {
  203.             case 0:  /* Screen Bar */
  204.                 ShowTitle(drawwin->WScreen, item->Flags & CHECKED);
  205.                 break;
  206.             case 1:  /* Tool Bar */
  207.                 /* Do the open or close */
  208.                 if(item->Flags & CHECKED)
  209.                 {
  210.                     /* If the open fails, stop immediately */
  211.                     if(!openToolWin())
  212.                         return FALSE;
  213.                 }
  214.                 else
  215.                     closeToolWin();
  216.                 break;
  217.             case 3:  /* Fractal (item 2 is the bar!) */
  218.                 drawFractal(drawwin);
  219.             }
  220.         }
  221.     }
  222.     /* Keep going */
  223.     return TRUE;
  224. }
  225.  
  226. static void new(struct Window* win)
  227. {
  228.     SetRast(win->RPort, 0);
  229. }
  230.  
  231. /* Our RDArgs structure for use with ReadArgs() */
  232. static struct RDArgs* myrdargs = NULL;
  233.  
  234. int createArgs()
  235. {
  236.     if(myrdargs = AllocDosObject(DOS_RDARGS, NULL))
  237.     {
  238.         /* Disable prompting on stdin when "?" is the argument */
  239.         myrdargs->RDA_Flags = RDAF_NOPROMPT;
  240.         return TRUE;
  241.     }
  242.     else
  243.         printf("Error: could not allocate args for ARexx commands\n");
  244.     return FALSE;
  245. }
  246.  
  247. void freeArgs()
  248. {
  249.     if(myrdargs)
  250.         FreeDosObject(DOS_RDARGS, myrdargs);
  251. }
  252.  
  253. /* The result of a ReadArgs() while parsing commands */
  254. static struct RDArgs* rdargs = NULL;
  255.  
  256. /* Test if a string matches a command, using ReadArgs() */
  257. static int isCommand(char* text, char* comm,
  258.                                          char* templ, LONG* args)
  259. {
  260.     int clen = strlen(comm);
  261.     if(strnicmp(text, comm, clen) == 0)
  262.     {
  263.         /* Is the command followed by some whitespace? */
  264.         if(text[clen] == ' ' || text[clen] == '\t')
  265.         {
  266.             int tlen = strlen(text);
  267.             /* Set up our myrdargs so we can use ReadArgs() */
  268.             myrdargs->RDA_Source.CS_Buffer = text+clen+1;
  269.             myrdargs->RDA_Source.CS_Length = tlen-clen;
  270.             myrdargs->RDA_Source.CS_CurChr = 0;
  271.             myrdargs->RDA_DAList = NULL;
  272.             myrdargs->RDA_Buffer = NULL;
  273.             /* Temporarily end the string with a return... */
  274.             /* (Needed to get ReadArgs() to work properly) */
  275.             text[tlen] = '\n';
  276.             rdargs = ReadArgs(templ, args, myrdargs);
  277.             /* ... now we must reinstate the string's terminator */
  278.             text[tlen] = '\0';
  279.             return rdargs != NULL;
  280.         }
  281.     }
  282.     return NULL;
  283. }
  284.  
  285. static void freeCommand()
  286. {
  287.     if(rdargs)
  288.     {
  289.         FreeArgs(rdargs);
  290.         rdargs = NULL;
  291.     }
  292. }
  293.  
  294. /* The maximum number of arguments for our commands */
  295. #define MAX_ARGS (3)
  296.  
  297. #define COMM_QUIT        "QUIT"
  298.  
  299. #define COMM_NEW        "NEW"
  300.  
  301. #define COMM_PEN        "PEN"
  302. #define TEMPL_PEN        "PEN/N"
  303. enum PEN_ARGS { PEN_PEN };
  304.  
  305. #define COMM_DRAW        "DRAW"
  306. #define TEMPL_DRAW    "X/N,Y/N,TEXT/F"
  307. enum DRAW_ARGS { DRAW_X, DRAW_Y, DRAW_TEXT };
  308.  
  309. /* Process an ARexx message */
  310. static int doARexx(struct RexxMsg* msg, struct Window* drawwin)
  311. {
  312.     int going = TRUE;
  313.     /* By default, our reply will indicate an error */
  314.     LONG rc = 20;
  315.     char* res = NULL;
  316.     char* command = msg->rm_Args[0];
  317.     /* Parse the command */
  318.     if(stricmp(command, COMM_QUIT) == 0)
  319.     {
  320.         going = FALSE;
  321.         /* We recognised the command, so set rc to zero */
  322.         rc = 0;
  323.         res = "Hello Painter is quitting";
  324.     }
  325.     else if(stricmp(command, COMM_NEW) == 0)
  326.     {
  327.         new(drawwin);
  328.         rc = 0;
  329.         res = "Display cleared";
  330.     }
  331.     else
  332.     {
  333.         LONG args[MAX_ARGS];
  334.         int i;
  335.         for(i=0; i<MAX_ARGS; i++)
  336.             args[i] = NULL;
  337.         if(isCommand(command, COMM_PEN, TEMPL_PEN, args))
  338.          {
  339.             /* args[0] holds the pen number to use */
  340.             LONG* nptr = (LONG*)(args[PEN_PEN]);
  341.             setFgPen(drawwin, *nptr);
  342.             rc = 0;
  343.             res = "Pen set";
  344.         }
  345.         else if(isCommand(command, COMM_DRAW, TEMPL_DRAW, args))
  346.          {
  347.             /* args[DRAW_X] and args[DRAW_Y] hold the coordinate */
  348.             /* args[DRAW_TEXT] holds the text to be drawn */
  349.             LONG* xptr = (LONG*)(args[DRAW_X]);
  350.             LONG* yptr = (LONG*)(args[DRAW_Y]);
  351.             char* text = (char*)(args[DRAW_TEXT]);
  352.             Move(drawwin->RPort, *xptr, *yptr);
  353.             Text(drawwin->RPort, text, strlen(text));
  354.             rc = 0;
  355.             res = "Text drawn";
  356.         }
  357.         freeCommand();
  358.     }
  359.     replyARexxMsg(msg, rc, res);
  360.     return going;
  361. }
  362.